$(function(){
    /*菜单添加active效果*/
    active();
    
    /*跳转*/
    $('.ul li').on('click', function () {
        click_url($(this));
        $(this).parent().children('li').removeClass('active');
        $(this).addClass('active');
    })
    
    /*分页*/
    l_page();
    
    /*搜索配置*/
    var select2config = {
        width: "300",
        tags: false,
        placeholder: "搜：客户id/姓名/电话/地址",
        allowClear: true,
        ajax: {
            url: "?m=index-Customer&do=search",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    search: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data, params) {
                // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data
                params.page = params.page || 1;
                return {
                    results: data.ext.items,
                    pagination: {
                        more: (params.page * 5) < data.ext.total_count
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) {
            return markup;
        } // let our custom formatter work
    };
    
    /*搜索*/
    $('#customer_select').select2(select2config);

}); 


/*菜单添加点击效果*/
function active(){
    var action = $('#left').attr('action');
    $('#'+action).addClass('active');
    
    var method = $('#left').attr('method');
    $('#'+method).addClass('active');
}

/*页面跳转*/
function click_url(ev){
    var url = ev.attr('url');
    this.location.href = url;
}

/*数据类型验证*/
function check(){
    var is_check = true;
    /*不能为空验证*/
    is_check =  check_require();
    if(is_check == false){
        return false;
    }
    
    /*手机验证*/
    is_check = check_mobile();
    if(is_check == false){
        return false;
    }
    
    /*整数验证*/
    is_check = check_int();
    if(is_check == false){
        return false;
    }
    
    /*整数验证*/
    is_check = check_float();
    if(is_check == false){
        return false;
    }
    
    return is_check;
}

/*不能为空*/
function check_require(){
    var err = true;
    $('.require').each(function(){
        if(!$(this).val()){
            err = false;
            var te = $(this).parent().prev().text();
            l_fail(te+"不能为空!");
            focus($(this));
            return false;
        }
    })
    return err;
}

function focus(ev){
    ev.focus();
    $('td').removeClass('focus');
    ev.parent('td').addClass('focus');
}

/*手机验证*/
function check_mobile(){
    var err = true;
    $('.mobile').each(function(){
        var val = $(this).val();
        if(isNaN(val) || val.length!=11){
            err = false;
            l_fail("手机格式不正确!");
            focus($(this));
            return false;
        }
    })
    return err;
}

/*整数验证*/
function check_int(){
    var err = true;
    $('.int').each(function(){
        var val = $(this).val();
        var z = /^[0-9]*$/;
        if(!z.test(val)){
            err = false;
            l_fail("必须为整数!");
            focus($(this));
            return false;
        }
    })
    return err;
}

/*浮点数验证*/
function check_float(){
    var err = true;
    $('.float').each(function(){
        var val = $(this).val();
        if(isNaN(val) || val<=0){
            err = false;
            l_fail("必须为数字!");
            focus($(this));
            return false;
        }
    })
    return err;
}

/*提示*/
function l_tip(title,icon,shift){
    layui.use(['layer'], function () {
        var layer = layui.layer;
        layer.msg(title, {icon: icon, offset: 0, shift: shift});
    });
}

/*弹出框*/
function l_alert(tit){
    layui.use(['layer'], function () {
        var layer = layui.layer;
        layer.confirm(tit, function (index) {
            layer.close(index);
        }); 
    });
}

/*分页*/
function l_page() {
    /*获取跳转地址*/
    var pages = $('#page_count').val();
    var url = location.href;
    var page = location.search.match(/page=(\d+)/);
    if(page){
        url = location.href.replace('&'+page[0],'');
    }
    //    console.log(url);
    layui.use(['laypage'], function () {
        var laypage = layui.laypage;
        laypage({
            cont: 'page',
            skin: '#333', //加载内置皮肤，也可以直接赋值16进制颜色值，如:#c00
            pages: pages, //可以叫服务端把总页数放在某一个隐藏域，再获取。假设我们获取到的是18
            groups: 8, //连续显示分页数
            curr: function () { //通过url获取当前页，也可以同上（pages）方式获取
                return page ? page[1] : 1;
            }(),
            jump: function (e, first) { //触发分页后的回调
                if (!first) { //一定要加此判断，否则初始时会无限刷新
                    location.href = url+'&page=' + e.curr;
                }
            }
        });
    });
}

/*成功提示*/
function l_succcess(title){
    l_tip(title,1,0);
}

/*错误提示*/
function l_fail(title){
    l_tip(title,2,6);
}

/*刷新页面*/
function reload_page(){
    window.location.reload();
}

/*页面跳转*/
function redir_url(url){
    this.location.href = url;
}

/*退出*/
function logout(){
    var url = '?m=index-Index&do=logout';
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data: '',
        success: function (res) {
//            console.log(res);
            if (res.code == 200)
            {
                var red_url = "?m=index-Index&do=index";
                redir_url(red_url);
            } else
            {
                l_fail(res.message);
            }
        }
    });
}